import { createRouter, createWebHistory } from 'vue-router'
import homeView from '@/views/home/home.vue'
import loginView from '@/views/login/index.vue'
import registerView from '@/views/register/index.vue'
import myView from '@/views/home/my.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/home', name: 'home', component: homeView, children: [
        { path: 'my', name: 'my', component: myView, },

      ]
    },
    { path: '/login', name: 'login', component: loginView, },
    { path: '/register', name: 'register', component: registerView, },

  ],
})
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  if (to.fullPath== '/home/my') {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
